package com.woniu.woniu_bx.sqlprovider;

import com.woniu.woniu_bx.pojo.Order;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author sjy
 * @create 2022-08-29 14:24
 */
public class OrderSqlProvider {
    public static String getByCondition(Order order) {
        String sql = new SQL() {
            {
                SELECT("o.id oid", "buy_time", "order_num", "state", "o.money om","pw_option","goods_id","num","account");
                FROM("bx_order o");
                LEFT_OUTER_JOIN("bx_user u on o.user_id=u.id");
                if (order != null) {
                    if (order.getOrderNum() != null) {
                        WHERE("order_num like CONCAT('%',#{orderNum},'%')");
                    }
                    if (order.getState() != null) {
                        WHERE("state=#{state}");
                    }
                    if (order.getUser() != null && order.getUser().getAccount() != null) {
                        WHERE("account like CONCAT('%',#{user.account},'%')");
                    }
                    if (order.getMinNum() != null) {
                        WHERE("num >= #{minNum}");
                    }
                    if (order.getMaxNum() != null) {
                        WHERE("num <= #{maxNum}");
                    }
                    if (order.getMinMoney() != null) {
                        WHERE("money >= #{minMoney}");
                    }
                    if (order.getMaxMoney() != null) {
                        WHERE("money <= #{maxMoney}");
                    }
                    if (order.getStartTime() != null) {
                        WHERE("buy_time >= #{startTime}");
                    }
                    if (order.getEndTime() != null) {
                        WHERE("buy_time <= #{endTime}");
                    }
                    if (order.getPwOption() != null) {
                        WHERE("pw_option=#{pwOption}");
                    }
                }
            }
        }.toString();
        System.out.println(sql);
        return sql;
    }
}
